package homework;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class Work2 {
    public static void main(String[] args) throws Exception {
        Connection connection =     WorkDButil.getConnection();
        String sql = " INSERT INTO authors (name,birth_year) VALUES (?,?) ";
        PreparedStatement ps = connection.prepareStatement(sql);
        ps.setString(1,"余华");
        ps.setString(2,"1960-2-2");
        int i = ps.executeUpdate();
        int id=0;
        if (i > 0) {
            sql = "SELECT id FROM authors WHERE name=?";
            ps = connection.prepareStatement(sql);
            ps.setString(1,"余华");
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                id = rs.getInt("id");
            }
        }

        if (id != 0) {
            sql ="INSERT INTO books (author_id,title,publish_year,price) VALUES (?,?,?,?)";
            ps = connection.prepareStatement(sql);
            ps.setInt(1,id);
            ps.setString(2,"活着");
            ps.setString(3,"1993-0-0");
            ps.setDouble(4,22.0);
            ps.executeUpdate();
        }
        connection.close();

    }
}
